<template>
    <div class="table-container">
        <div class="content-table">
            <el-table
                :data="tableData"
                border
                size="small"
                style="width: 100%"
                header-row-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column
                    type="selection"
                    width="90"
                    align="center">
                </el-table-column>
                <el-table-column
                    label="姓名"
                    align="center">
                    <template slot-scope="scope">
                        <div class="name-pic" v-if="scope.row.next == 'true'" @click="toReviewResultsAgain(scope.row)">
<!--                            <img src="../../assets/reviews/name-pic.png"/>-->
                            <p>{{scope.row.name}}</p>
                            <img src="../../assets/reviews/icon_yulan.png"/>
                        </div>
                        <div v-else>{{scope.row.name}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="currentTitle"
                    label="现职称"
                    align="center">
                </el-table-column>
                <el-table-column
                    prop="participatingTitle"
                    label="参评职称"
                    align="center">
                </el-table-column>
                <el-table-column
                    prop="postType"
                    label="参评岗位"
                    align="center">
                </el-table-column>
                <el-table-column
                    prop="total"
                    label="得票"
                    align="center">
                </el-table-column>
            </el-table>
        </div>
        <div class="footer-bar">
            <el-button :disabled="isChecked" @click="reviewAgain" type="primary" size="small" round>进行再次评审</el-button>
            <template v-if="tableParams.rounds == '1'">
                <el-button :disabled="isChecked" @click="towReviewAgain" size="small" round>进行二轮评审</el-button>
            </template>
        </div>
    </div>
</template>

<script>
import Qs from 'qs'
import { findResultList,findCfg ,intoAgain} from '@/service/api'
export default {
    name: "reviewResultsTable",
    data() {
        return {
            tableData:[],
            isTowReview:false,
            isChecked:true,
            userIds:[],
            findCfgData:{}
        }
    },
    props:{
        tableParams:Object
    },
    created() {
        console.log(this.tableParams)
        this.inits();
        this.handleFindCfg()
    },
    methods: {
        inits() {
            let params = this.tableParams
            findResultList(Qs.stringify(params)).then(res => {  //筛选查询数据
                if (res.code == 200) {
                    this.tableData = res.data;
                } else {
                    console.log(res)
                }
            });
        },
        handleSelectionChange(e) {
            if(e.length>0){
                this.isChecked = false;
            }else {
                this.isChecked = true;
            }
            this.userIds = e;
        },
        handleFindCfg() {
            findCfg().then(res => {  //查询第几轮
                if (res.code == 200) {
                    this.findCfgData = res.data
                    const params = this.tableParams;
                    if(params.rounds == '1' && res.data[params.typeStatus].length>1){
                        console.log(true)
                        this.isTowReview = true;
                    }
                    // console.log(res.data[params.typeStatus])
                } else {
                    console.log(res)
                }
            });
        },
        reviewAgain() { //进入二次评审
            let userIds = [],
                params = {},
                next = true;
            for(let item of this.userIds){
                if(item.next == 'true'){
                    this.$message('已选人员已经进入再次评审中，请勿重复操作！');
                    next = false
                }
                userIds.push(item.id)
            }
            if(!next)
                return false;
            params.userIds = JSON.stringify(userIds);
            params.rounds = this.tableParams.rounds+'-1';
            params.typeStatus = this.tableParams.typeStatus;
            this.postIntoAgain(params)
        },
        towReviewAgain() {
            // if(!this.isTowReview){
            //     this.$message('第二轮评审未配置！');
            //     return false;
            // }
            let typeStatus = this.tableParams.typeStatus;
            let cfgData = this.findCfgData[typeStatus]
            // if(cfgData.length >= 2 && new Date(cfgData[1].startTime) < new Date()){
            //     this.$message('第二轮评审已开始！');
            //     return false;
            // }
            let userIds = [],
                params = {},
                next = true;
            for(let item of this.userIds){
                if(item.nextTwo == 'true'){
                    this.$message('已选人员已经进入第二轮评审中，请勿重复操作！');
                    next = false;
                    break;
                }
                userIds.push(item.id)
            }
            if(!next)
                return false;
            params.userIds = JSON.stringify(userIds);
            params.rounds = '2';
            params.typeStatus = this.tableParams.typeStatus;
            this.postIntoAgain(params)
        },
        postIntoAgain(params) {
            intoAgain(Qs.stringify(params)).then(res => {
                if (res.code == 200) {
                    this.$message({
                        message: '执行成功!',
                        type: 'success'
                    });
                    this.inits();
                } else {
                    console.log(res)
                }
            });
        },
        toReviewResultsAgain() {
            let query = Object.assign({},this.tableParams)
            query.rounds = query.rounds+'-1'
            console.log(query)
            let routeData = this.$router.resolve({ path: '/reviewResultsAgain', query: query});
            window.open(routeData.href, '_blank');
        }

    },
    watch:{
        'tableParams':{
            deep: true,
            handler:function(value) {   //特别注意，不能用箭头函数，箭头函数，this指向全局
                this.params = value;
                this.inits()
            },
        },
    }
}
</script>

<style scoped>
.table-container{
    height: calc(100% - 170px);
}
.content-table{
    height: 100%;
    margin: 13px 25px;
    border-radius: 5px 5px 0px 0px;
    overflow: hidden;
}
.content-table >>> .table-header th {
    background: #E6EAEB !important;
    border: none;
}
.content-table >>> .table-header th div{
    font-weight:bold !important;
    color: #333;
}

.content-table >>> .table-header .el-checkbox {
    position: relative;
    width: 100%;
    display: block;
    margin-left: -18px;
}

.content-table >>> .table-header .el-checkbox:before {
    content: "全选";
    position: absolute;
    left: 50px;
    font-weight: bold !important;
    color: #333;
}
.el-form{
    margin-top: 18px;
}
.content-table >>> .is-round{
    padding: 6px 15px;
}
.content-table >>>.el-table--small td{
    padding: 7px 0;
}
.footer-bar{
    display: flex;
    margin: 0 auto;
    justify-content: center;
}
.footer-bar > button{
    display: block;
    margin: 0 20px;
}
.name-pic{
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}
.name-pic img{
    display: block;
    width: 18px;
    margin-left: 10px;
}
</style>
